<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-calendar"></i> 管理员管理
        </el-breadcrumb-item>
        <el-breadcrumb-item>管理员日志</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <el-table
        :data="tableData"
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="username" label="管理员" align="center"></el-table-column>
        <el-table-column label="动作" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.type=='error'" :style="{color:'red'}">{{scope.row.action}}</span>
            <span v-if="scope.row.type=='login'" :style="{color:'green'}">{{scope.row.action}}</span>
            <span v-if="scope.row.type=='logout'" :style="{color:'blue'}">{{scope.row.action}}</span>
            <span v-if="scope.row.type=='info'" :style="{color:'black'}">{{scope.row.action}}</span>
          </template>
        </el-table-column>
         <el-table-column prop="request_data" inline="true" label="请求数据" align="center"></el-table-column>
        <el-table-column prop="time" label="时间" align="center"></el-table-column>
        <el-table-column prop="ip" label="IP" align="center"></el-table-column>

      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.page"
          :page-size="query.pageSize"
          :total="total"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
      <el-form ref="form" :model="form" label-width="70px">
        <el-form-item label="用户名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="用户ID">
          <el-input v-model="form.custom_id"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="头像">
          <el-input v-model="form.head_img"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "basetable",
  data() {
    return {
      query: {
        name: "",
        page: 1,
        pageSize: 10
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      total: 0,
      form: {},
      idx: -1,
      id: -1
    };
  },
  created() {
    this.getData();
  },
  methods: {
    // 获取 easy-mock 的模拟数据
    getData() {
      this.$api.admin.log(this.query).then(res => {
        this.tableData = res.data;
        this.total = res.total || 0;
      });
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "page", 1);
      this.getData();
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning"
      })
        .then(() => {

          var arr = [row.id];
           this.$api.user.delete({ ids: arr }).then(res => {
            if (res.code == 1) {
              this.$message.error("删除失败");
              return;
            }
            this.getData();
            this.$message.success("删除成功");
          });
        })
        .catch(() => {});
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delAllSelection() {
      const length = this.multipleSelection.length;
      var arr = [];
      this.delList = this.delList.concat(this.multipleSelection);
      for (let i = 0; i < length; i++) {
        arr.push(this.multipleSelection[i].id);
      }
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning"
      })
        .then(() => {
          this.$api.user.delete({ ids: arr }).then(res => {
            if (res.code == 1) {
              this.$message.error("删除失败");
              return;
            }
            this.getData();
            this.$message.success("删除成功");
          });
        })
        .catch(() => {});
      this.multipleSelection = [];
    },
    // 编辑操作
    handleEdit(index, row) {
      this.idx = index;
      this.form = row;
      this.editVisible = true;
    },
    // 保存编辑
    saveEdit() {
      this.$api.user.update(this.form).then((res)=>{
        if(res.code==1){
          this.$message.error(`修改失败`);
          return;
        }
        this.editVisible = false;
      this.$message.success(`修改成功`);
      this.$set(this.tableData, this.idx, this.form);
      });

    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "page", val);
      this.getData();
    },

  }
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
